/*
 * Copyright (C) 2013 AndXmppSDK Project <hljdrl@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drl.xmpp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.DelayInformation;

/**
 * TODO: 用一句话描述这个类(接口)的作用
 *
 * @author dai.rui.lin
 * @date 2012-7-31 下午1:31:35
 */
public class StringUtils2 {
	
	 /**
     * Modifies Wildcards such as <b>%random%</b>,<b>%system%</b>,
     * <b>%version%</b>,<b>%time%</b> into what they represent
     * <p>
     * for properties see System.getProperties()
     * 
     * @param resource
     *            String to modify
     * @return modified String
     * @author wolf.posdorfer
     */
    public static String modifyWildcards(String resource) {
	if(resource==null)
	    return "";
	
	resource = resource.replace("%random%",
		"" + Math.round((Math.random() * 1000)));

	resource = resource.replace("%system%", System.getProperty("os.name")
		.replace(" ", ""));

	resource = resource.replace("%version%",
		System.getProperty("os.version").replace(" ", ""));

	final String DATE_FORMAT_NOW = "HH:mm";
	Calendar cal = Calendar.getInstance();
	SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

	resource = resource.replace("%time%", sdf.format(cal.getTime()));

	while (resource.contains("%property{")) {
	    if (resource.contains("%property{") && resource.contains("}/%")) {
		int indexofbeginning = resource.indexOf("%property{");
		int indexofending = resource.indexOf("}/%");

		String begin = resource.substring(0, indexofbeginning);
		String middle = System.getProperty(resource.substring(
			indexofbeginning+10, indexofending));
		String end = resource.substring(indexofending + 3);
		resource = begin + middle + end;
	    }
	}

	return resource;
    }
    private static final SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static String formDateXMPP(Date date){
    	return form.format(date);
    }
    public static Date parseDateXMPP(String date) throws ParseException{
    	if(StringHelper.isText(date)){
    	return form.parse(date);
    	}else {
    		return null;
    	}
    	
    }
    
    public static Date parseDateXMPP2(String date) throws ParseException{
    	if(StringHelper.isText(date)){
    	return mTrendform.parse(date);
    	}else {
    		return null;
    	}
    	
    }
    
    private static final SimpleDateFormat mTrendform = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
    
    public static String formatTrendTime(String time){
    	if(time==null)return mTrendform.format(new Date(System.currentTimeMillis()));
    	try {
			Date d = StringUtils.parseXEP0082Date(time);
			return mTrendform.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return time;
    	
    }
    public static String formatTrendTime(Date time){
    	if(time==null)return mTrendform.format(new Date(System.currentTimeMillis()));
			return mTrendform.format(time);
    	
    }
    /**
     * 时间格式化
     * @param context 上下文
     * @param targetTimeMillis 时间
     * @return 格式化之后的时间字符串
     */
    public static String formatDate(Date date) {
    	if(date==null)return null;
    	
    	SimpleDateFormat form = new SimpleDateFormat("HH:mm:ss");
    	Date arg0 =new Date(System.currentTimeMillis());
    	if(arg0.getDay() == date.getDay() && arg0.getMonth() == date.getMonth()){
    	form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	}
    	
    	return form.format(date);
	}
    
    
    public static String formateDataOfflineMessageKey(Message msg)
    {
    	Date mTimestamp = null; 
    	PacketExtension pTime = msg.getExtension("delay", "urn:xmpp:delay");
		if (pTime instanceof DelayInformation) {
			mTimestamp = ((DelayInformation) pTime).getStamp();
		} else {
			mTimestamp = new Date();
		}
		StringBuffer buf = new StringBuffer();
		buf.append(msg.getFrom());
		buf.append(mTrendform.format(mTimestamp));
		return buf.toString();
    }
    
    public static String parseBareAddress2(String XMPPAddress) {
		if (XMPPAddress == null) {
			return null;
		}
		int slashIndex = XMPPAddress.indexOf("/");
		if (slashIndex < 0) {
			return XMPPAddress;
		} else if (slashIndex == 0) {
			return XMPPAddress;
		} else {
			return XMPPAddress.substring(0, slashIndex);
		}
	}
    
    public static String parseName(String XMPPAddress) {
		if (XMPPAddress == null) {
			return null;
		}
		int atIndex = XMPPAddress.lastIndexOf("@");
		if (atIndex <= 0) {
			return XMPPAddress;
		} else {
			return XMPPAddress.substring(0, atIndex);
		}
	}
    
    
    public static boolean isTrue(String str){
    	if(str ==null){
    		return false;
    	}else if(str.equals("") || str.equals("0")){
    		return false;
    	}else if(str.equals("1")){
    		return true;
    	}else {
    		return false;
    	}
    }
    

}
